package dump;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;

public class User {
	public static abstract class AbstractCollection {
		protected Cursor cursor;

		public AbstractCollection(final Cursor cursor) {
			this.cursor = cursor;
		}

		public void close() {
			this.cursor.close();
		}

		public abstract ContentValues cursorToValues(Cursor cursor);

		public ContentValues next() {
			final ContentValues values = null;

			if (this.cursor.moveToNext()) {
				return this.cursorToValues(this.cursor);
			}

			return values;
		}
	}

	public static class ContactMethodsCollection extends AbstractCollection {
		public ContactMethodsCollection(final Cursor cursor) {
			super(cursor);
		}

		@Override
		public ContentValues cursorToValues(final Cursor cursor) {
			return User.cursorToContactMethodsValues(cursor);
		}
	}

	public static class PhoneCollection extends AbstractCollection {
		public PhoneCollection(final Cursor cursor) {
			super(cursor);
		}

		@Override
		public ContentValues cursorToValues(final Cursor cursor) {
			return User.cursorToPhoneValues(cursor);
		}
	}

	/**
	 * UserCollection
	 * 
	 * Inner class wrapping a Cursor over Contacts
	 */
	public static class UserCollection extends AbstractCollection {
		public UserCollection(final Cursor cursor) {
			super(cursor);
		}

		@Override
		public ContentValues cursorToValues(final Cursor cursor) {
			return User.cursorToUserValues(cursor);
		}

	}

	static final String[] baseProjection = new String[] {
			android.provider.BaseColumns._ID,
			android.provider.Contacts.PeopleColumns.DISPLAY_NAME,
			android.provider.Contacts.PeopleColumns.NOTES,
			android.provider.Contacts.PeopleColumns.STARRED };

	static final String[] contactMethodsProjection = new String[] {
			android.provider.BaseColumns._ID,
			android.provider.Contacts.ContactMethodsColumns.DATA,
			android.provider.Contacts.ContactMethodsColumns.AUX_DATA,
			android.provider.Contacts.ContactMethodsColumns.KIND,
			android.provider.Contacts.ContactMethodsColumns.LABEL,
			android.provider.Contacts.ContactMethodsColumns.TYPE,
			android.provider.Contacts.ContactMethodsColumns.ISPRIMARY };

	static final String[] phonesProjection = new String[] {
			android.provider.BaseColumns._ID,
			android.provider.Contacts.PhonesColumns.LABEL,
			android.provider.Contacts.PhonesColumns.NUMBER,
			android.provider.Contacts.PhonesColumns.NUMBER_KEY,
			android.provider.Contacts.PhonesColumns.TYPE };

	/**
	 * create
	 * 
	 * Create a new Contact.
	 * 
	 * @param resolver
	 * @param values
	 * @return
	 */
	public static String create(final ContentResolver resolver,
			final ContentValues values) {
		if (resolver == null) {
			return null;
		}
		if (values == null) {
			return null;
		}
		final Uri uri = Contacts.People.createPersonInMyContactsGroup(resolver,
				values);
		// Uri uri = resolver.insert(Contacts.People.CONTENT_URI, values);
		return String.valueOf(ContentUris.parseId(uri));
	}

	private static ContentValues cursorToContactMethodsValues(
			final Cursor cursor) {
		if (cursor == null) {
			return null;
		}

		final ContentValues values = new ContentValues();
		String val;
		val = cursor.getString(cursor
				.getColumnIndex(Contacts.ContactMethodsColumns.DATA));
		values.put(Contacts.ContactMethodsColumns.DATA, val);
		val = cursor.getString(cursor
				.getColumnIndex(Contacts.ContactMethodsColumns.AUX_DATA));
		values.put(Contacts.ContactMethodsColumns.AUX_DATA, val);
		val = cursor.getString(cursor
				.getColumnIndex(Contacts.ContactMethodsColumns.LABEL));
		values.put(Contacts.ContactMethodsColumns.LABEL, val);
		Integer intVal = new Integer(cursor.getInt(cursor
				.getColumnIndex(Contacts.ContactMethodsColumns.ISPRIMARY)));
		values.put(Contacts.ContactMethodsColumns.ISPRIMARY, intVal);
		intVal = new Integer(cursor.getString(cursor
				.getColumnIndex(Contacts.ContactMethodsColumns.KIND)));
		values.put(Contacts.ContactMethodsColumns.KIND, intVal);
		intVal = new Integer(cursor.getString(cursor
				.getColumnIndex(Contacts.ContactMethodsColumns.TYPE)));
		values.put(Contacts.ContactMethodsColumns.TYPE, intVal);
		return values;
	}

	private static ContentValues cursorToPhoneValues(final Cursor cursor) {
		if (cursor == null) {
			return null;
		}

		final ContentValues values = new ContentValues();
		String val;
		val = cursor.getString(cursor
				.getColumnIndex(Contacts.PhonesColumns.LABEL));
		values.put(Contacts.PhonesColumns.LABEL, val);

		val = cursor.getString(cursor
				.getColumnIndex(Contacts.PhonesColumns.NUMBER));
		values.put(Contacts.PhonesColumns.NUMBER, val);

		final Integer intVal = new Integer(cursor.getInt(cursor
				.getColumnIndex(Contacts.PhonesColumns.TYPE)));
		values.put(Contacts.PhonesColumns.TYPE, intVal);

		return values;
	}

	private static ContentValues cursorToUserValues(final Cursor cursor) {
		if (cursor == null) {
			return null;
		}

		final ContentValues values = new ContentValues();
		String val;
		val = cursor.getString(cursor
				.getColumnIndex(android.provider.BaseColumns._ID));
		values.put(android.provider.BaseColumns._ID, val);

		val = cursor.getString(cursor
				.getColumnIndex(Contacts.PeopleColumns.DISPLAY_NAME));
		values.put(Contacts.PeopleColumns.DISPLAY_NAME, val);

		final Integer intVal = new Integer(cursor.getInt(cursor
				.getColumnIndex(Contacts.PeopleColumns.STARRED)));
		values.put(Contacts.PeopleColumns.STARRED, intVal);

		val = cursor.getString(cursor
				.getColumnIndex(Contacts.PeopleColumns.NOTES));
		values.put(Contacts.PeopleColumns.NOTES, val);
		return values;
	}

	/**
	 * delete
	 * 
	 * Delete a Contact.
	 * 
	 * @param resolver
	 * @param id
	 */
	public static void delete(final ContentResolver resolver, final String id) {
		if (id == null) {
			return;
		}

		resolver.delete(Uri.withAppendedPath(Contacts.People.CONTENT_URI, id),
				null, null);
	}

	/**
	 * get
	 * 
	 * Get a single Contact.
	 * 
	 * @param resolver
	 * @param id
	 * @return
	 */
	public static ContentValues get(final ContentResolver resolver,
			final String id) {
		if (id == null) {
			return null;
		}

		final String[] whereArgs = new String[] { id };
		final Cursor cursor = resolver.query(Contacts.People.CONTENT_URI,
				User.baseProjection, "people."
						+ android.provider.BaseColumns._ID + " = ?", whereArgs,
				Contacts.PeopleColumns.NAME + " ASC");
		cursor.moveToFirst();
		final ContentValues values = User.cursorToUserValues(cursor);
		cursor.close();
		return values;
	}

	/**
	 * getAll
	 * 
	 * Get all Contacts.
	 * 
	 * @param resolver
	 * @return
	 */
	public static UserCollection getAll(final ContentResolver resolver) {
		return new UserCollection(resolver.query(Contacts.People.CONTENT_URI,
				User.baseProjection, null, null, null));
	}

	/**
	 * getContactMethods
	 * 
	 * Get the ContactMethods for a Contact.
	 * 
	 * @param resolver
	 * @param id
	 * @return
	 */
	public static ContactMethodsCollection getContactMethods(
			final ContentResolver resolver, final String id) {
		if (id == null) {
			return null;
		}

		final String[] whereArgs = new String[] { id };
		return new ContactMethodsCollection(resolver.query(
				Contacts.ContactMethods.CONTENT_URI,
				User.contactMethodsProjection, "people."
						+ android.provider.BaseColumns._ID + " = ?", whereArgs,
				Contacts.ContactMethodsColumns.KIND + " DESC"));
	}

	/**
	 * getPhones
	 * 
	 * Get the phone numbers for a Contact.
	 * 
	 * @param resolver
	 * @param id
	 * @return
	 */
	public static PhoneCollection getPhones(final ContentResolver resolver,
			final String id) {
		if (id == null) {
			return null;
		}

		final String[] whereArgs = new String[] { id };
		return new PhoneCollection(resolver.query(Contacts.Phones.CONTENT_URI,
				User.phonesProjection, "people."
						+ android.provider.BaseColumns._ID + " = ?", whereArgs,
				Contacts.PhonesColumns.TYPE + " ASC"));
	}

	/**
	 * save
	 * 
	 * Update an existing Contact.
	 * 
	 * @param resolver
	 * @param values
	 * @param id
	 */
	public static void save(final ContentResolver resolver,
			final ContentValues values, final String id) {
		if (resolver == null) {
			return;
		}
		if (values == null) {
			return;
		}
		if (id == null) {
			return;
		}
		final Uri uri = Uri.withAppendedPath(Contacts.People.CONTENT_URI, id);
		resolver.update(uri, values, null, null);
	}
}
